package pl.edu.agh.inpema.components.receivers.randomReceiver;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import javax.jms.JMSException;

import org.apache.log4j.Logger;

import pl.edu.agh.inpema.core.components.ReceiverUtil;
import pl.edu.agh.inpema.core.configuration.ConfigurationException;
import pl.edu.agh.inpema.core.dataBase.DataBase;
import pl.edu.agh.inpema.core.model.Message;
import pl.edu.agh.inpema.core.model.Profile;
import pl.edu.agh.inpema.core.model.Receiver;
import pl.edu.agh.inpema.core.queue.Queue;
import pl.edu.agh.inpema.core.queue.QueueMessage;

public class RandomReceiver extends Thread implements Receiver {
	
	private int timeout = 250;;
	private int dataNumber = 20;
	
	private boolean isRunning = false;
	
	private boolean error = false;
	
	private static Logger logger = Logger.getLogger(RandomReceiver.class);
	
	private Random rand = new Random();
	
	private ReceiverUtil receiverUtil;
	
	private static final String[] logins = {
		"login1", "login2", "login3", "login4"
	};
	private static final String[] ips = {
		"192.168.1.1", "192.168.1.2", "192.168.1.3", "192.168.1.4", "192.168.1.5", "192.168.1.6"
	};
	private static final String[] texts = {
		"aaa http://www.agh.edu.pl", "bbb http://www.onet.pl", "ccc http://www.google.pl"
	};
	private static final String[] dates = {
		"30.05.2008 10:20:32", "20.05.2008 10:20:32", "10.05.2008 10:20:32"
	};
	
	private static final String[] messageFieldTypes = {
		FIELD_IP, FIELD_DATE, FIELD_TEXT
	};
	
	public void setReceiverUtil(ReceiverUtil util) {
		this.receiverUtil = util;
	}
	
	public ReceiverUtil getReceiverUtil() {
		return this.receiverUtil;
	}
	
	public void run() {
		logger.info(this.receiverUtil.getReceiverName() + ": starting...");
		try {
			DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
			int number = 0;
			while (this.isRunning) {
				number++;
				if (number > this.dataNumber) {
					logger.info(this.receiverUtil.getReceiverName() + ": no more data");
					this.isRunning = false;
				} else {
					
					String login = logins[rand.nextInt(logins.length)];
					
					Date date = df.parse(dates[rand.nextInt(dates.length)]);
					
					Map<String, String> messageFields = new HashMap<String, String>();
					messageFields.put(Message.FIELD_IP, ips[rand.nextInt(ips.length)]);
					messageFields.put(Message.FIELD_DATE, ""+date.getTime());
					messageFields.put(Message.FIELD_TEXT, texts[rand.nextInt(texts.length)]);
					
					this.receiverUtil.addMessage(login, messageFields);
					
					logger.info(this.receiverUtil.getReceiverName() + ": send message");
				}
				try {
					Thread.sleep(timeout);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		} catch (Exception e) {
			error = true;
			e.printStackTrace();
		}
		logger.info(this.receiverUtil.getReceiverName() + ": finishing...");
	}
	
	public void startGathering() {
		this.isRunning = true;
		this.start();
	}
	
	public boolean stopGathering() {
		this.isRunning = false;
		return this.error;
	}
	
	public boolean isGatheringRunning() {
		return this.isRunning;
	}
	
	public void setDataNumber(int dataNumber) {
		this.dataNumber = dataNumber;
	}
	
	public void setTimeouut(int timeout) {
		this.timeout = timeout;
	}
	
	public String[] getMessageFieldTypes() {
		return messageFieldTypes;
	}
}
